local string_format = string.format

----------------------------------------------------------------
--通用接口
----------------------------------------------------------------
local function FormatMsg(fmt, arg1, ...)
	if not arg1 then return fmt end
	local msg
	local args = {...}
	xpcall(function()
		for k, v in ipairs(args) do
			args[k] = tostring(v)
		end
		msg = string_format(fmt, arg1, table.unpack(args))
	end)
	return msg
end

local traceFunc = traceback.dumpstr
local function TraceMsg(msg, traceLevel)
	local traceInfo = traceFunc(traceLevel or 0)
	return string.format("%s, %s", msg, traceInfo)
end

-- debug.excepthook专用，和Error区别在于堆栈的获取层级
function Error2ExceptHook(...)
	local key = FormatMsg(...)
	if not key then return end
	local content = TraceMsg(key, 5)
	print(content)
end

function Info(...)
    local info = debug.getinfo(3)
    if info then
        print(string_format("[file]=%s,[line]=%d:",info.source or "?",info.currentline or 0), ...)
    end
end

function Debug(...)
    local info = debug.getinfo(3)
    if info then
        print(string_format("[file]=%s,[line]=%d:",info.source or "?",info.currentline or 0), ...)
    end
end

function Warn(...)
    local info = debug.getinfo(3)
    if info then
        print(string_format("[file]=%s,[line]=%d:",info.source or "?",info.currentline or 0), ...)
    end
end

function Error(...)
    local info = debug.getinfo(3)
    if info then
        print(string_format("[file]=%s,[line]=%d:",info.source or "?",info.currentline or 0), ...)
    end
end




function _INFO(...)
    Info(...) 
end

function _DEBUG(...) 
    Debug(...) 
end

function _WARN(...) 
    Warn(...)
end

function _RUNTIME_INFO(...)
    Info(...)
end
function _ERROR(...) 
    Error(...)
end